package com.sjz.controller;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.util.Arrays;

public class FaceCompare {

    // 初始化人脸探测器
    static CascadeClassifier faceDetector;

    private static final String PATH_PREFIX = "D:\\11\\audio\\";

	static int i = 0;

    static {
        // 判断系统
        String os = System.getProperty("os.name");

        // 加载动态库
        if (os != null && os.toLowerCase().startsWith("windows")) {
            // Windows操作系统
            // todo windows 系统部署加载 .dll 文件 - 路径跟据自己存放位置更改【这里需要使用绝对路径】
           System.load("E:\\ziliao\\project\\opencv\\opencv\\opencv\\src\\main\\resources\\lib\\opencv_java420.dll");
           //System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        } else if (os != null && os.toLowerCase().startsWith("linux")) {
            // Linux操作系统
            // todo Linux 服务器部署加载 .so 文件 - 路径跟据自己存放位置更改【是否需要绝对路径有待验证，目前只在windows 系统实践过】
            System.load("/opt/face/libopencv_java440.so");
        }

        // 引入 特征分类器配置 文件：haarcascade_frontalface_alt.xml 文件路径
        // 此文件在opencv的安装目录build\etc\haarcascades下可以找到
        String property = "E:\\ziliao\\project\\opencv\\opencv\\opencv\\haarcascade_frontalface_alt.xml";
        System.out.println(property);
        faceDetector = new CascadeClassifier(property);
    }

    public static void main(String[] args) {
    	 comparePicture();
    	 
    }
    
    
    /****
             * 比较两张图片
     */
    public static void comparePicture() {
    	  // 图片路径不能包含中文
        String str1 = PATH_PREFIX + "5.jpg";
        String str2 = PATH_PREFIX + "6.jpg";
        long start = System.currentTimeMillis();
        double compareHist = compare_image(str1, str2);
        System.out.println("time:" + (System.currentTimeMillis() - start));
        System.out.println(compareHist);
        if (compareHist > 0.6) {
            System.out.println("人脸匹配");
        } else {
            System.out.println("人脸不匹配");
        }
    }

    // 灰度化人脸
    public static Mat conv_Mat(String img) {
        Mat image0 = Imgcodecs.imread(img);
        Mat image1 = new Mat();
        // 灰度化
        Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY);
        // 探测人脸
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image1, faceDetections);
        // rect中人脸图片的范围
        for (Rect rect : faceDetections.toArray()) {
            Mat face = new Mat(image1, rect);
            return face;
        }
        return null;
    }

    // 比较图片
    public static double compare_image(String img_1, String img_2) {
        Mat mat_1 = conv_Mat(img_1);
        Mat mat_2 = conv_Mat(img_2);
        Mat hist_1 = new Mat();
        Mat hist_2 = new Mat();
        //颜色范围
        MatOfFloat ranges = new MatOfFloat(0f, 256f);
        //直方图大小， 越大匹配越精确 (越慢)
        MatOfInt histSize = new MatOfInt(10000000);
        Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
        Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
        // CORREL 相关系数
        double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
        return res;
    }



}
